<?php
class WeSql{

    protected $pdo;


    /**
     * @param $array
     * @return array
     */
    function arrayGetValue($array){
        $arr = Array();
        for($i = 0;$i<count(array_keys($array));$i++){
            $arr[$i] = (is_numeric($array[array_keys($array)[$i]])) ?  ($array[array_keys($array)[$i]]): ("'".$array[array_keys($array)[$i]]."'");
        }
        return $arr;
    }

    /**
     * //处理sql传进来的数据，对不是数字的自动添加上引号
     * @param $array
     * @return array
     */
    function arrayTranslationQuotes($array){
        $arr = Array();
        for($i = 0;
            $i<count($array);
            $i++){
            $arr[array_keys($array)[$i]] = (is_numeric($array[array_keys($array)[$i]])) ? ($array[array_keys($array)[$i]]) : ("'".$array[array_keys($array)[$i]]."'");
        }
        return $arr;
    }

    /**
     * 处理更新数组
     * @param $array 数组
     * @return array
     */
    function UpdateProcessingArray($array){
        $array = $this->arrayTranslationQuotes($array);
        $arr = Array();
        for($i = 0;
            $i<count($array);
            $i++){ //键名 = 值
            $arr[$i] = array_keys($array)[$i]."=".$array[array_keys($array)[$i]];
        }
        return $arr;
    }

    /**
     * WeSql constructor.
     * @param $config
     */
    public function __construct($config)
    {
        try{
            $this->pdo = new PDO("mysql:host=${config['host']};dbname=${config['name']}",$config['user'],$config['pass']);;
        }catch(PDOException $e){
            echo "数据库连接失败";
        }

    }

    /*
     *@Result 查询表名字段名
     *{
     *  $tableName 表名
     * }
     * @Return array
     */
    function selectFieldName($tableName){
        $re = $this->pdo->prepare("select * from ${tableName}");
        $re->execute();
        return array_keys($re->fetch(2));
    }


    /**
     * 获取数据表全部数据
     * @param $tableName 表名
     * @return array
     */
    function selectAll($tableName, $conditions=1,$num=null){
        if($num == null){
            $num = $this->getCount($tableName);
        }
        $re = $this->pdo->prepare("select * from ${tableName} where ${conditions} LIMIT ${num}");
        $re->execute();
        return ($re->fetchAll());
    }

    /**
     * @param $tableName 表名
     * @param int $conditions 条件
     * @param $flag
     * @return mixed
     */
    function select($tableName, $conditions=1,$flag = 2){
        $re = $this->pdo->prepare("select * from ${tableName} where ${conditions}");
        $re->execute();
        return ($re->rowCount()>0) ? $re->fetch($flag) : [];
    }



    /**
     * @param $tableName 表名
     * @param array $field 字段数组{name=>value}
     * @return bool
     */
    function isSelectField($tableName, $field=[]){
        $field = $this->arrayTranslationQuotes($field);
        $re = $this->pdo->prepare("select * from ${tableName} where ".array_keys($field)[0]."=".$field[array_keys($field)[0]]);
        $re->execute();//执行
        //columnCount()获取字段数量 及列
        return ($re->rowCount())? true:false;
    }

    /*@Result 插入数据
     *{
     *  $tableName 表名
     *  $parameter 字段数组{name=>value}
     * }
     * @Return boolean
     */


    function insert($tableName,$parameter = []){
        $sql = sprintf("insert into %s (%s) value (%s)",
            $tableName,
            implode(', ',array_keys($parameter)),//给每个数据数据添加分割符号到到效果
            implode(', ',$this->arrayGetValue($parameter))
        );
        try{
            $re = $this->pdo->prepare($sql);
            $re->execute($parameter);
            return ($re->rowCount() == 1)?true:false;
        }catch(PDOException $e){
            echo("错误");
            return false;
        }
    }



    function update($tableName , $parameter = [],$conditions = 1){
        $sql = sprintf("UPDATE %s SET %s WHERE %s",
            $tableName,
            implode(', ',$this->UpdateProcessingArray($parameter)),
            $conditions
        );
        try{
            $re = $this->pdo->prepare($sql);
            return ($re->execute($parameter))?true:false;
        }catch(PDOException $e){
            echo("错误");
            return false;
        }
    }

    /**
     * @param $tableName 表名
     * @param int $conditions 条件
     * @return int 表数量
     */
    function getCount($tableName, $conditions = 1){
        $re = $this->pdo->prepare("select * from ${tableName} where ${conditions}");
        $re->execute();
        return $re->rowCount();
    }
}



//include "config.php";
//$weSql = new WeSql($config,$key);
//print_r($weSql->demo([
//    "package" => "package",
//    "title" => "title",
//    "content" => "",
//    "version" => "",
//    "time" => date("Y-m-d h:i:s")
//]));